﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace StarWarsGame
{
    public class ImageButton : DrawableGameComponent
    {      
        SpriteBatch spriteBatch;
        Texture2D btnNormal, btnOver, cursor;
        MouseState mouseState;
        int mousex;
        int mousey;
        SpriteFont myfont;
        int imageXPos;
        int imageYPos;

        public bool SecondImageSelected;

        public ImageButton(Game game, Texture2D firstTexture, Texture2D secondTexture,SpriteBatch currentSpriteBatch, int xpos, int ypos)
            : base(game)
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = currentSpriteBatch;
            btnNormal = firstTexture;
            btnOver = secondTexture;
            cursor = game.Content.Load<Texture2D>("cursor");
            myfont = game.Content.Load<SpriteFont>("menuSmall");
            imageXPos = xpos;
            imageYPos = ypos;

            //load the content

        }    

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            mouseState = Mouse.GetState();

            mousex = mouseState.X;
            mousey = mouseState.Y;
            //get the mouse x y valuves in to varibles


            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {        
            //start the spitebatch process
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
            if(SecondImageSelected)
            {
                spriteBatch.Draw(btnOver, new Rectangle(imageXPos, imageYPos, 150, 150), Color.White);//and draw the mouse over image
            }
            else{
            spriteBatch.Draw(btnNormal, new Rectangle(imageXPos, imageYPos, 150, 150), Color.White);//draw normal button
            }

            if ((mousex < imageXPos + 150 && mousex > imageXPos) && (mousey > imageYPos && mousey < imageYPos + 150))//identify mouse over x y posotions for the button
            {               

                if (ButtonState.Pressed == mouseState.LeftButton)
                {
                    if (!SecondImageSelected)
                    {
                        spriteBatch.Draw(btnOver, new Rectangle(imageXPos, imageYPos, 150, 150), Color.White);//and draw the mouse over image
                    }
                    else
                    {
                        spriteBatch.Draw(btnNormal, new Rectangle(imageXPos, imageYPos, 150, 150), Color.White);//draw normal button
                    }
                    SecondImageSelected = !SecondImageSelected;
                }
            }
            spriteBatch.Draw(cursor, new Vector2(mousex, mousey), Color.White);
            //draw the cursor
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
